/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.appengine.lib.server.login;

import nl.biz_e.appengine.lib.client.services.GoogleLoginService;
import nl.biz_e.appengine.lib.shared.login.GoogleLoginInfo;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Implementation of the {@link GoogleLoginService} interface
 * 
 * @author Joost A. Bloemsma
 */
public class GoogleLoginServiceImpl extends RemoteServiceServlet implements
		GoogleLoginService {
	
	private static final long serialVersionUID = -4954032435383513062L;
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public GoogleLoginInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		GoogleLoginInfo loginInfo = new GoogleLoginInfo();
		
		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setAuthDomain(user.getAuthDomain());
			loginInfo.setGoogleId(user.getUserId());
			loginInfo.setAdmin(userService.isUserAdmin());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}
	
}
